package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.service.impl.RoleServiceImpl;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.query.EmployeeQuery;
import cn.wolfcode.crm.service.impl.DepartmentServiceImpl;
import cn.wolfcode.crm.service.impl.EmployeeServiceImpl;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2020/3/12.
 */
@Controller
@RequestMapping("/employee")
public class EmployeeController {

	@Autowired
	private EmployeeServiceImpl employeeService;

	@Autowired
	private DepartmentServiceImpl departmentService;

	@Autowired
	private RoleServiceImpl roleService;
	@RequestMapping("/list")
	@RequiresPermissions(value = {"employee:list","员工列表"},logical = Logical.OR)
	public String list(Model model, @ModelAttribute("qo") EmployeeQuery qo) {
		model.addAttribute("result", employeeService.queryForList(qo));
		//添加部门信息
		model.addAttribute("depts", departmentService.listAll());
		return "employee/list";
	}
	@RequestMapping("/input")
	@RequiresPermissions(value = {"employee:input","员工编辑"},logical = Logical.OR)
	public String input(Model model, Long id) {
		if (id != null) {
			Employee employee = employeeService.get(id);
			model.addAttribute("employee", employee);
		}
		List<Department> depts = departmentService.listAll();
		List<Role> roles = roleService.listAll();
		//查询出所有部门存入到模型对象中
		model.addAttribute("depts", depts);
		//查询出所有角色存入到模型对象中
		model.addAttribute("roles", roles);
		return "employee/input";
	}

	@RequestMapping("/saveOrUpdate")
	@RequiresPermissions(value = {"employee:saveOrUpdate","员工保存或更新"},logical = Logical.OR)
	@ResponseBody
	public JsonResult saveOrUpdate(Employee employee, Long[] ids) {
		employeeService.saveOrUpdate(employee, ids);
		return new JsonResult(true,"操作成功");
	}
	@RequestMapping("/delete")
	@RequiresPermissions(value = {"employee:delete","员工删除"},logical = Logical.OR)
	public String delete(Long id) {
		if (id != null) {
			employeeService.delete(id);
		}
		return "redirect:/employee/list.do";
	}

	@RequestMapping("/batchDelete")
	@ResponseBody
	@RequiresPermissions(value = {"employee:batchDelete","员工批量删除"},logical = Logical.OR)
	public JsonResult batchDelete(Long[] ids) {
		if(ids !=null){
		employeeService.batchDelete(ids);
		return new JsonResult(true,"删除成功");
		}else{
			return new JsonResult(false,"删除失败");
		}
	}

	@RequestMapping("/checkName")
	@ResponseBody
	public HashMap checkName(String name) {
		Employee employee = employeeService.getByName(name);
		HashMap<String, Boolean> map = new HashMap<>();
		map.put("valid",employee==null);
		return map;
	}
}
